<template>
  <div class="com_contalner">
    <el-card class="box-card" shadow="never" style="margin-top: 20px">
      <div class="left">
        <el-icon><Tickets /></el-icon>
        <p>数据列表</p>
      </div>
      <el-button @click="onAdd">添加</el-button>
    </el-card>

    <div class="tab">
      <el-table
        :border="true"
        style="width: 100%"
        :data="tableData"
        :cell-style="cellstyle"
        :header-cell-style="headercellstyle"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />

        <el-table-column prop="id" label="编号" width="100" />
        <el-table-column prop="name" label="原因类型" width="500" />
        <el-table-column label="排序" width="80" prop="sort" />
        <el-table-column label="是否可用" width="500">
          <template #default="scoped">
            <el-switch
              :model-value="scoped.row.status == 1 ? true : false"
              @change="onChange(scoped.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="" label="添加时间" width="200">
          <template #default="scoped">
            <div>
              {{ dayjs(scoped.row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scoped">
            <el-button type="primary" link @click="edit(scoped.row)"
              >编辑</el-button
            >
            <el-button type="primary" link @click="del(scoped.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="bottom">
      <div class="left">
        <el-select v-model="value" placeholder="批量删除" size="large">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" style="margin-left: 10px" @click="quedin">
          确定</el-button
        >
      </div>
      <div class="right">
        <!-- 分页器 -->
        <el-pagination
          v-model:currentPage="pagination.pageNum"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[1, 5, 10, 20]"
          layout="total,sizes, prev, pager, next,jumper"
          :total="pagination.total"
        />
      </div>
    </div>

    <el-dialog v-model="dialogFormVisible" title="添加退货原因" width="30%">
      <el-form :model="form">
        <el-form-item label="原因类型：" label-width="120px">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="排序：" label-width="120px">
          <el-input v-model="form.sort" autocomplete="off" />
        </el-form-item>
        <el-form-item label="是否启用：" label-width="120px">
          <el-switch v-model="form.status" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="onSure"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { Search, Tickets } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import type {
  returnReasonUpdateStatusRes,
  IreturnReasonList,
} from "@/request/OmsOrderReturnApplyController";
import useTable from "@/hooks/useTable";
import * as api from "@/request/api";
import dayjs from "dayjs";
import { ElMessage, ElMessageBox } from "element-plus";
import "element-plus/es/components/message-box/style/css";
import "element-plus/es/components/message/style/css";
const value = ref("");
const options = [
  {
    value: "1",
    label: "删除",
  },
];

let form = reactive({
  name: "",
  sort: 0,
  status: 1,
  id: 0,
  createTime: "",
});
let {
  tableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
  getTableData,
} = useTable(api.returnApply.getReturnReasonList);
let cellstyle = () => {
  return {
    textAlign: "center",
    padding: "10px",
  };
};
let headercellstyle = () => {
  return {
    textAlign: "center",
    color: "#303133",
  };
};

const onChange = async (row: returnReasonUpdateStatusRes) => {
  console.log("onChange");
  let ids = row.id;
  // 如果 原状态值为0则 status为1 ，反之亦然。
  let status: 0 | 1 = row.status === 0 ? 1 : 0;
  await api.returnApply.returnReasonUpdateStatus({ ids, status });
  ElMessage({
    message: "状态修改成功!",
    type: "success",
  });
  row.status = status;
};

const dialogFormVisible = ref(false);
let edit = (value: {
  name: string;
  sort: number;
  status: number;
  id: number;
  createTime: string;
}) => {
  // 编辑的确定按钮
  flag.value = true;
  dialogFormVisible.value = true;
  form.name = value.name;
  form.sort = value.sort;
  form.status = value.status;
  form.id = value.id;
  form.createTime = value.createTime;
};

// 删除的方法
let del = async (val: {
  name: string;
  sort: number;
  status: number;
  id: number;
  createTime: string;
}) => {
  await api.returnApply.returnReasondelete(val.id);
  getTableData();
};

// 是否为编辑
let flag = ref(false);
let onSure = async () => {
  let status: number = form.status === true ? 1 : 0;
  if (flag.value) {
    await api.returnApply.returnReasonUpdate({
      id: form.id,
      name: form.name,
      status: status,
      sort: form.sort,
      createTime: form.createTime,
    });
  } else {
    await api.returnApply.returnReasonCreate({
      name: form.name,
      status: status,
      sort: form.sort,
      createTime: form.createTime,
    });
  }

  //关闭弹窗
  dialogFormVisible.value = false;
  // 提示修改成功
  ElMessage({
    message: "修改成功！",
    type: "success",
  });
  // 重新发一个页面的请求
  getTableData();
};

let onAdd = () => {
  // 这是添加的方法
  flag.value = false;
  dialogFormVisible.value = true;
  form.name = "";
  form.sort = 0;
  form.status = 1;
};

let newArr = ref([]);
// 删除全部
let handleSelectionChange = (val: IreturnReasonList[]) => {
  newArr.value = val.map((item) => {
    return item.id;
  });
  console.log("arr", newArr.value);
};

let quedin = async () => {
  console.log(value.value);
  if (value.value == "1") {
    if (newArr.value.length == 0) {
      ElMessage({
      message: "请选择要操作的条目!",
      type: "warning",
    });
      return;
    }
    console.log("我要删除");
    await api.returnApply.returnReasondelete(newArr.value);
    getTableData()
  } else {
    // 弹窗
    ElMessage({
      message: "请选择要操作的条目!",
      type: "warning",
    });
  }

  // if (newArr.value.length) {
  //   ElMessage({
  //     message: "请选择要操作的条目!",
  //     type: "warning",
  //   });
  // }
};
</script>

<style lang="scss" scoped>
.com_contalner {
  padding: 10px 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
}
.left {
  display: flex;
  align-items: center;
  p {
    margin-left: 8px;
  }
}
.tab {
  margin: 20px 0;
}
.bottom {
  display: flex;
  justify-content: space-between;
}
:deep .el-card__body {
  display: flex;
  justify-content: space-between;
}
</style>
